//
//  ActivityVCViewController.m
//  豆瓣
//
//  Created by lanou3g on 15/12/9.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "ActivityVCViewController.h"
#import "HTActivityTVCell.h"
#import "ActivityDetailsVC.h"
#import "ActivityModel.h"

#define URL @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"

@interface ActivityVCViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *dataArray;  //装model得数据数组

@end

@implementation ActivityVCViewController

NSString *activity_cell_id = @"activity_cell_id";



//处理数据
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        self.dataArray = [NSMutableArray array];
        NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            
            
           
                
                //解析
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                
                for (NSDictionary *di in dic[@"events"]) {
                    ActivityModel *model = [ActivityModel new];
                    [model setValuesForKeysWithDictionary:di];
                    
                    [self.dataArray addObject:model];
                   
                }
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.htTableView.tableView reloadData];
                    
                });
         
            
            
            
    
            
        }];
        
        [dataTask resume];
        
        
    }
    
    return self;
}



//视图已经出现
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    [self.HUD hide:NO];     //小菊花停止
}



- (void)loadView
{
    self.htTableView = [[HTTableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.htTableView;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setTableView];

   
    
}



//设置表示图
- (void)setTableView
{
    
    //注册cell
    [self.htTableView.tableView registerClass:[HTActivityTVCell class] forCellReuseIdentifier:activity_cell_id];
    
    self.htTableView.tableView.delegate = self;
    self.htTableView.tableView.dataSource = self;
    
}


#pragma mark 表视图代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.dataArray.count;
  
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HTActivityTVCell *cell = [tableView dequeueReusableCellWithIdentifier:activity_cell_id];
    
    cell.model = self.dataArray[indexPath.section];
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 150;
}




- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}



#pragma mark cell点击跳转

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailsVC *detailsVC = [ActivityDetailsVC new];
    detailsVC.model_detail = self.dataArray[indexPath.section]; //model传值
    [self.navigationController pushViewController:detailsVC animated:YES];
}



@end










